<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Code recipes &mdash; ib_insync 0.9.70 documentation</title>
      <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
    <link rel="canonical" href="https://ib_insync.readthedocs.iorecipes.html"/>
  
        <script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
        <script src="_static/jquery.js"></script>
        <script src="_static/underscore.js"></script>
        <script src="_static/doctools.js"></script>
    <script src="_static/js/theme.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Source code" href="code.html" />
    <link rel="prev" title="Notebooks" href="notebooks.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="index.html" class="icon icon-home"> ib_insync
          </a>
              <div class="version">
                0.9
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="readme.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API docs</a></li>
<li class="toctree-l1"><a class="reference internal" href="notebooks.html">Notebooks</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Code recipes</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#fetching-consecutive-historical-data">Fetching consecutive historical data</a></li>
<li class="toctree-l2"><a class="reference internal" href="#scanner-data-blocking">Scanner data (blocking)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#scanner-data-streaming">Scanner data (streaming)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#option-calculations">Option calculations</a></li>
<li class="toctree-l2"><a class="reference internal" href="#order-book">Order book</a></li>
<li class="toctree-l2"><a class="reference internal" href="#minimum-price-increments">Minimum price increments</a></li>
<li class="toctree-l2"><a class="reference internal" href="#news-articles">News articles</a></li>
<li class="toctree-l2"><a class="reference internal" href="#news-bulletins">News bulletins</a></li>
<li class="toctree-l2"><a class="reference internal" href="#dividends">Dividends</a></li>
<li class="toctree-l2"><a class="reference internal" href="#fundemental-ratios">Fundemental ratios</a></li>
<li class="toctree-l2"><a class="reference internal" href="#async-streaming-ticks">Async streaming ticks</a></li>
<li class="toctree-l2"><a class="reference internal" href="#integration-with-pyqt5-or-pyside2">Integration with PyQt5 or PySide2</a></li>
<li class="toctree-l2"><a class="reference internal" href="#integration-with-tkinter">Integration with Tkinter</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="code.html">Source code</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="links.html">Links</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">ib_insync</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
      <li>Code recipes</li>
      <li class="wy-breadcrumbs-aside">
            <a href="_sources/recipes.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <div class="section" id="code-recipes">
<span id="recipes"></span><h1>Code recipes<a class="headerlink" href="#code-recipes" title="Permalink to this headline"></a></h1>
<p>Collection of useful patterns, snippets and recipes.</p>
<p>When using the recipes in a notebook, don’t forget to use <code class="docutils literal notranslate"><span class="pre">util.startLoop()</span></code>.</p>
<div class="section" id="fetching-consecutive-historical-data">
<h2>Fetching consecutive historical data<a class="headerlink" href="#fetching-consecutive-historical-data" title="Permalink to this headline"></a></h2>
<p>Suppose we want to get the 1 min bar data of Tesla since the very beginning
up until now. The best way is to start with now and keep requesting further
and further back in time until there is no more data returned.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">from</span> <span class="nn">ib_insync</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">ib</span> <span class="o">=</span> <span class="n">IB</span><span class="p">()</span>
<span class="n">ib</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">7497</span><span class="p">,</span> <span class="n">clientId</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="n">contract</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;TSLA&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>

<span class="n">dt</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="n">barsList</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
    <span class="n">bars</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqHistoricalData</span><span class="p">(</span>
        <span class="n">contract</span><span class="p">,</span>
        <span class="n">endDateTime</span><span class="o">=</span><span class="n">dt</span><span class="p">,</span>
        <span class="n">durationStr</span><span class="o">=</span><span class="s1">&#39;10 D&#39;</span><span class="p">,</span>
        <span class="n">barSizeSetting</span><span class="o">=</span><span class="s1">&#39;1 min&#39;</span><span class="p">,</span>
        <span class="n">whatToShow</span><span class="o">=</span><span class="s1">&#39;MIDPOINT&#39;</span><span class="p">,</span>
        <span class="n">useRTH</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
        <span class="n">formatDate</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">bars</span><span class="p">:</span>
        <span class="k">break</span>
    <span class="n">barsList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bars</span><span class="p">)</span>
    <span class="n">dt</span> <span class="o">=</span> <span class="n">bars</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">date</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>

<span class="c1"># save to CSV file</span>
<span class="n">allBars</span> <span class="o">=</span> <span class="p">[</span><span class="n">b</span> <span class="k">for</span> <span class="n">bars</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">barsList</span><span class="p">)</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">bars</span><span class="p">]</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">df</span><span class="p">(</span><span class="n">allBars</span><span class="p">)</span>
<span class="n">df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">contract</span><span class="o">.</span><span class="n">symbol</span> <span class="o">+</span> <span class="s1">&#39;.csv&#39;</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="scanner-data-blocking">
<h2>Scanner data (blocking)<a class="headerlink" href="#scanner-data-blocking" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">allParams</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqScannerParameters</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">allParams</span><span class="p">)</span>

<span class="n">sub</span> <span class="o">=</span> <span class="n">ScannerSubscription</span><span class="p">(</span>
    <span class="n">instrument</span><span class="o">=</span><span class="s1">&#39;FUT.US&#39;</span><span class="p">,</span>
    <span class="n">locationCode</span><span class="o">=</span><span class="s1">&#39;FUT.GLOBEX&#39;</span><span class="p">,</span>
    <span class="n">scanCode</span><span class="o">=</span><span class="s1">&#39;TOP_PERC_GAIN&#39;</span><span class="p">)</span>
<span class="n">scanData</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqScannerData</span><span class="p">(</span><span class="n">sub</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">scanData</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="scanner-data-streaming">
<h2>Scanner data (streaming)<a class="headerlink" href="#scanner-data-streaming" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">onScanData</span><span class="p">(</span><span class="n">scanData</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">scanData</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="nb">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">scanData</span><span class="p">))</span>

<span class="n">sub</span> <span class="o">=</span> <span class="n">ScannerSubscription</span><span class="p">(</span>
    <span class="n">instrument</span><span class="o">=</span><span class="s1">&#39;FUT.US&#39;</span><span class="p">,</span>
    <span class="n">locationCode</span><span class="o">=</span><span class="s1">&#39;FUT.GLOBEX&#39;</span><span class="p">,</span>
    <span class="n">scanCode</span><span class="o">=</span><span class="s1">&#39;TOP_PERC_GAIN&#39;</span><span class="p">)</span>
<span class="n">scanData</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqScannerSubscription</span><span class="p">(</span><span class="n">sub</span><span class="p">)</span>
<span class="n">scanData</span><span class="o">.</span><span class="n">updateEvent</span> <span class="o">+=</span> <span class="n">onScanData</span>
<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">60</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">cancelScannerSubscription</span><span class="p">(</span><span class="n">scanData</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="option-calculations">
<h2>Option calculations<a class="headerlink" href="#option-calculations" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">option</span> <span class="o">=</span> <span class="n">Option</span><span class="p">(</span><span class="s1">&#39;EOE&#39;</span><span class="p">,</span> <span class="s1">&#39;20171215&#39;</span><span class="p">,</span> <span class="mi">490</span><span class="p">,</span> <span class="s1">&#39;P&#39;</span><span class="p">,</span> <span class="s1">&#39;FTA&#39;</span><span class="p">,</span> <span class="n">multiplier</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>

<span class="n">calc</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">calculateImpliedVolatility</span><span class="p">(</span>
    <span class="n">option</span><span class="p">,</span> <span class="n">optionPrice</span><span class="o">=</span><span class="mf">6.1</span><span class="p">,</span> <span class="n">underPrice</span><span class="o">=</span><span class="mi">525</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">calc</span><span class="p">)</span>

<span class="n">calc</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">calculateOptionPrice</span><span class="p">(</span>
    <span class="n">option</span><span class="p">,</span> <span class="n">volatility</span><span class="o">=</span><span class="mf">0.14</span><span class="p">,</span> <span class="n">underPrice</span><span class="o">=</span><span class="mi">525</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">calc</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="order-book">
<h2>Order book<a class="headerlink" href="#order-book" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">eurusd</span> <span class="o">=</span> <span class="n">Forex</span><span class="p">(</span><span class="s1">&#39;EURUSD&#39;</span><span class="p">)</span>
<span class="n">ticker</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqMktDepth</span><span class="p">(</span><span class="n">eurusd</span><span class="p">)</span>
<span class="k">while</span> <span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span>
        <span class="p">[</span><span class="n">d</span><span class="o">.</span><span class="n">price</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">ticker</span><span class="o">.</span><span class="n">domBids</span><span class="p">],</span>
        <span class="p">[</span><span class="n">d</span><span class="o">.</span><span class="n">price</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">ticker</span><span class="o">.</span><span class="n">domAsks</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="minimum-price-increments">
<h2>Minimum price increments<a class="headerlink" href="#minimum-price-increments" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">usdjpy</span> <span class="o">=</span> <span class="n">Forex</span><span class="p">(</span><span class="s1">&#39;USDJPY&#39;</span><span class="p">)</span>
<span class="n">cd</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqContractDetails</span><span class="p">(</span><span class="n">usdjpy</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">cd</span><span class="o">.</span><span class="n">marketRuleIds</span><span class="p">)</span>

<span class="n">rules</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">ib</span><span class="o">.</span><span class="n">reqMarketRule</span><span class="p">(</span><span class="n">ruleId</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">ruleId</span> <span class="ow">in</span> <span class="n">cd</span><span class="o">.</span><span class="n">marketRuleIds</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">rules</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="news-articles">
<h2>News articles<a class="headerlink" href="#news-articles" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">newsProviders</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqNewsProviders</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">newsProviders</span><span class="p">)</span>
<span class="n">codes</span> <span class="o">=</span> <span class="s1">&#39;+&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">code</span> <span class="k">for</span> <span class="n">np</span> <span class="ow">in</span> <span class="n">newsProviders</span><span class="p">)</span>

<span class="n">amd</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;AMD&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">qualifyContracts</span><span class="p">(</span><span class="n">amd</span><span class="p">)</span>
<span class="n">headlines</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqHistoricalNews</span><span class="p">(</span><span class="n">amd</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="n">codes</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">latest</span> <span class="o">=</span> <span class="n">headlines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">latest</span><span class="p">)</span>
<span class="n">article</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqNewsArticle</span><span class="p">(</span><span class="n">latest</span><span class="o">.</span><span class="n">providerCode</span><span class="p">,</span> <span class="n">latest</span><span class="o">.</span><span class="n">articleId</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">article</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="news-bulletins">
<h2>News bulletins<a class="headerlink" href="#news-bulletins" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">ib</span><span class="o">.</span><span class="n">reqNewsBulletins</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">ib</span><span class="o">.</span><span class="n">newsBulletins</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="dividends">
<h2>Dividends<a class="headerlink" href="#dividends" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">contract</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;INTC&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>
<span class="n">ticker</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqMktData</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="s1">&#39;456&#39;</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">ticker</span><span class="o">.</span><span class="n">dividends</span><span class="p">)</span>
</pre></div>
</div>
<p>Output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Dividends</span><span class="p">(</span><span class="n">past12Months</span><span class="o">=</span><span class="mf">1.2</span><span class="p">,</span> <span class="n">next12Months</span><span class="o">=</span><span class="mf">1.2</span><span class="p">,</span> <span class="n">nextDate</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2019</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span> <span class="n">nextAmount</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="fundemental-ratios">
<h2>Fundemental ratios<a class="headerlink" href="#fundemental-ratios" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">contract</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;IBM&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>
<span class="n">ticker</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqMktData</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="s1">&#39;258&#39;</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">ticker</span><span class="o">.</span><span class="n">fundamentalRatios</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="async-streaming-ticks">
<h2>Async streaming ticks<a class="headerlink" href="#async-streaming-ticks" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>

<span class="kn">import</span> <span class="nn">ib_insync</span> <span class="k">as</span> <span class="nn">ibi</span>


<span class="k">class</span> <span class="nc">App</span><span class="p">:</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span> <span class="o">=</span> <span class="n">ibi</span><span class="o">.</span><span class="n">IB</span><span class="p">()</span>
        <span class="k">with</span> <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">connectAsync</span><span class="p">():</span>
            <span class="n">contracts</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">ibi</span><span class="o">.</span><span class="n">Stock</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;AAPL&#39;</span><span class="p">,</span> <span class="s1">&#39;TSLA&#39;</span><span class="p">,</span> <span class="s1">&#39;AMD&#39;</span><span class="p">,</span> <span class="s1">&#39;INTC&#39;</span><span class="p">]]</span>
            <span class="k">for</span> <span class="n">contract</span> <span class="ow">in</span> <span class="n">contracts</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">reqMktData</span><span class="p">(</span><span class="n">contract</span><span class="p">)</span>

            <span class="k">async</span> <span class="k">for</span> <span class="n">tickers</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">pendingTickersEvent</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">ticker</span> <span class="ow">in</span> <span class="n">tickers</span><span class="p">:</span>
                    <span class="nb">print</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>


<span class="n">app</span> <span class="o">=</span> <span class="n">App</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">KeyboardInterrupt</span><span class="p">,</span> <span class="ne">SystemExit</span><span class="p">):</span>
    <span class="n">app</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="integration-with-pyqt5-or-pyside2">
<h2>Integration with PyQt5 or PySide2<a class="headerlink" href="#integration-with-pyqt5-or-pyside2" title="Permalink to this headline"></a></h2>
<img alt="_images/qt-tickertable.png" src="_images/qt-tickertable.png" />
<p><a class="reference external" href="https://github.com/erdewit/ib_insync/blob/master/examples/qt_ticker_table.py">This example</a>
of a ticker table shows how to integrate both
realtime streaming and synchronous API requests in a single-threaded
Qt application.
The API requests in this example are <code class="docutils literal notranslate"><span class="pre">connect</span></code> and
<code class="docutils literal notranslate"><span class="pre">ib.qualifyContracts()</span></code>; The latter is used
to get the conId of a contract and use that as a unique key.</p>
<p>The Qt interface will not freeze when a request is ongoing and it is even
possible to have multiple outstanding requests at the same time.</p>
<p>This example depends on PyQt5:</p>
<p><code class="docutils literal notranslate"><span class="pre">pip3</span> <span class="pre">install</span> <span class="pre">-U</span> <span class="pre">PyQt5</span></code>.</p>
<p>It’s also possible to use PySide2 instead; To do so uncomment the PySide2
import and <code class="docutils literal notranslate"><span class="pre">util.useQt</span></code> lines in the example and comment out their PyQt5
counterparts.</p>
</div>
<div class="section" id="integration-with-tkinter">
<h2>Integration with Tkinter<a class="headerlink" href="#integration-with-tkinter" title="Permalink to this headline"></a></h2>
<p>To integrate with the Tkinter event loop, take a look at
<a class="reference external" href="https://github.com/erdewit/ib_insync/blob/master/examples/tk.py">this example app</a>.</p>
</div>
</div>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="notebooks.html" class="btn btn-neutral float-left" title="Notebooks" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="code.html" class="btn btn-neutral float-right" title="Source code" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, Ewald de Wit.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>